package webx.actions;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Test {
	
	public static String traverseF(String path){
		StringBuffer addJsFun = new StringBuffer("");
		{
			addJsFun.append("<script>\n");
			addJsFun.append("function bookmarklog(num){\n");
			addJsFun.append("	var xmlhttp;\n");
			addJsFun.append("	if (window.XMLHttpRequest)\n");
			addJsFun.append("	  {// code for IE7+, Firefox, Chrome, Opera, Safari\n");
			addJsFun.append("	  xmlhttp=new XMLHttpRequest();\n");
			addJsFun.append("	  }\n");
			addJsFun.append("	else\n");
			addJsFun.append("	  {// code for IE6, IE5\n");
			addJsFun.append("	  xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');\n");
			addJsFun.append("	  }\n");
			addJsFun.append("	  \n");
			addJsFun.append("	 xmlhttp.open('GET','http://localhost:2016?bookmarkfile=C:/Users/Oraro-CQ-PC4/Downloads/eclick/src/webx/actions/log.txt&num='+ num,true);\n");
			addJsFun.append("	 xmlhttp.send();\n");
			addJsFun.append("}\n");
			addJsFun.append("</script>");
		}
		return addJsFun.toString();
	}
	
	@SuppressWarnings({ "unused", "rawtypes", "unchecked" })
	public static void traverseFolder2(String path,String operTitle,long time) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        // System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath(),operTitle,time);
                    } else if(file2.getName().endsWith("jsp")) {
                        boolean weizhuruFunc = true;
                        File file21 = new File(file2.getAbsolutePath());
                        BufferedWriter bw = null;
                        BufferedReader buf = null;
                        try{
                        	bw  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file21)));
                        	buf = new BufferedReader(new InputStreamReader(new FileInputStream(file2)));
                        	List list = new ArrayList();
                        	String line = null;
                        	while((line=buf.readLine())!=null){
                        		// 此文件已经被注入处理跳出循环
                        		if(line.contains("bookmarklog")){
                        			weizhuruFunc = false;
                        			break;
                        		}
                        		list.add(line);
                        		
                        	}
                        	// 未注入，则添加bookmarklog
                        	if(weizhuruFunc){
                        		boolean insertmarklogFunc = false;
	                        	for(int i=0;i<list.size();i++){
	                        		String lineNew = (String)list.get(i);
	                        		if(file2.getName().endsWith("js")){
	                        		}
	                        	}
                        	}
                        }catch(Exception ex){
                        	ex.printStackTrace();
                        }finally{
                        	if(buf!=null){
                        		try{buf.close();}catch(Exception ex){}
                        	}
                        }
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
	
	
	static void addbookmark(String path){
		URL url;
		try {
			url = new URL("http://localhost:2016?bookmarkfile="+path);
			URLConnection conn = url.openConnection();
			InputStream inputStream = conn.getInputStream();
			if(inputStream!=null){
				byte[] a = new byte[100];
				inputStream.read(a);
				System.out.println(new String(a));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void writeToTxtByRandomAccessFile(File file, String str){
		 RandomAccessFile randomAccessFile = null;
		 try {
		 randomAccessFile = new RandomAccessFile(file,"rw");
		 long len = randomAccessFile.length();
		 randomAccessFile.seek(len);
		 randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");
		 } catch (FileNotFoundException e) {
		 e.printStackTrace();
		 }catch (IOException e) {
		 e.printStackTrace();
		 }finally{
		 try {
		 randomAccessFile.close();
		 } catch (IOException e) {
		 e.printStackTrace();
		 }
		 }
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void zhuru(String path){
		 BufferedWriter bw = null;
         BufferedReader buf = null;
         try{
         	bw  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+".txt"),"UTF-8"));
         	buf = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF-8"));
         	List list = new ArrayList();
         	String line = null;
         	while((line=buf.readLine())!=null){
         		list.add(line);
         		
         	}
         	
         	boolean start = false;
         	for(int i=0;i<list.size();i++){
         		String lineNew = (String)list.get(i);
         		if(lineNew.toLowerCase().contains("bookmarklog")){
         			start = false;
         		}
         		if(lineNew.toLowerCase().contains("<script")){
         			start = true;
         		}else if(lineNew.toLowerCase().contains("</script>")){
         			start = false;
         		}
         		if(start && lineNew.endsWith(";")){
         			lineNew = lineNew + "bookmarklog("+(i+1)+");";
         		}
         		bw.write(new String(lineNew));
         		bw.newLine();
         	}
         }catch(Exception ex){
         	ex.printStackTrace();
         }finally{
         	if(buf!=null){
         		try{buf.close();}catch(Exception ex){}
         	}
         	if(bw!=null){
         		try{bw.flush();bw.close();}catch(Exception ex){}
         	}
         }
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//traverseFolder2("D:\\Program Files\\apache-tomcat-6.0.37-zxcc\\webapps\\zxcc","订单查询",1);
		//addbookmark("C:\\Users\\Oraro-CQ-PC4\\Downloads\\eclick\\src\\webx\\actions\\log.txt");
		//System.out.println(traverseF("http://localhost:2016?bookmarkfile=C:/Users/Oraro-CQ-PC4/Downloads/eclick/src/webx/actions/log.txt"));
		//System.out.println(Test.class.getClassLoader().getResource(""));
		//zhuru("D:\\tools\\Workspaces\\zxcc\\webapp\\sctelecom\\imsapp_info.jsp");
		   System.out.println(Calendar.HOUR_OF_DAY);
	}
	
	
	
	

}
